package com.carleasoft.mps.cms.business.write.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.cms.business.write.bean.entity.LeadingViewEntity;
import com.carleasoft.mps.cms.business.write.dao.LeadingViewMapper;
import com.carleasoft.mps.cms.business.write.service.LeadingViewService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Objects;

/**
 * @author: yangHao
 * @createDate:2023/6/9
 * @description:
 */
@Service
public class LeadingViewServiceImpl extends ServiceImpl<LeadingViewMapper, LeadingViewEntity> implements LeadingViewService {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addView(LeadingViewEntity leadingViewEntity) {
        boolean isOk = false;
        LeadingViewEntity viewEntity = this.getOne(Wrappers.<LeadingViewEntity>lambdaQuery()
                .eq(LeadingViewEntity::getWriteCode, leadingViewEntity.getWriteCode())
                .eq(LeadingViewEntity::getViewDate, LocalDate.now()));
        if (Objects.nonNull(viewEntity)) {
            viewEntity.setViewOne(viewEntity.getViewOne() + 1);
            isOk = this.updateById(viewEntity);
        } else {
            leadingViewEntity.setViewDate(LocalDate.now());
            leadingViewEntity.setViewOne(1);
            isOk = this.save(leadingViewEntity);
        }
        return isOk;
    }
}
